package com.ketangpai.admin.controller;

import com.ketangpai.admin.entity.User;
import com.ketangpai.admin.entity.param.LoginParam;
import com.ketangpai.admin.service.UserService;
import com.ketangpai.admin.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public JsonResult<?> login(
            @RequestBody LoginParam param
    ) throws Exception {
        User user = userService.login(param);
        return JsonResult.success(user, "登录成功");
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public JsonResult<?> register(@RequestBody User user) throws Exception {
        user.setAccount(user.getPhone());
        String b = userService.register(user);
        if (b.equals("注册成功")) {
            return JsonResult.success("注册成功");
        } else if(b.equals("账户重复")) {
            return JsonResult.error("账户重复");
        }else if(b.equals("请输入正确的手机号"))return JsonResult.error("请输入正确的手机号");
        else {
            return JsonResult.error("注册失败");
        }
    }

    @ApiOperation("根据id查询")
    @GetMapping("/selectById/{id}")
    public JsonResult<?> selectById(@PathVariable Integer id) {
        User user = userService.selectById(id);
        return JsonResult.success(user);
    }

    @ApiOperation("根据账号查询")
    @GetMapping("/selectByAccount/{account}")
    public JsonResult<?> selectByAccount(@PathVariable String account) {
        User user = userService.selectByAccount(account);
        return JsonResult.success(user);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public JsonResult<?> update(@RequestBody User user) {
        boolean b = userService.update(user);
//        return JsonResult.success();
        if (b) {
            return JsonResult.success(user.getFileImage(), "编辑成功");
        } else {
            return JsonResult.error("编辑失败");
        }
    }
}
